﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.Install
{
    public partial class Step2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        public static string InitialSystemValidCheck(ref bool error)
        {
            error = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("<table cellSpacing='0' cellPadding='0' width='90%' align='center' border='0' bgcolor='#666666' style='font-size:12px'>");

            HttpContext context = HttpContext.Current;
            string FileName = string.Empty;
            if (context != null)
            {
                FileName = context.Server.MapPath("~/Config/CNVP.JShop.config");
            }
            else
            {
                FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/CNVP.JShop.config");
            }
            //系统Dll完整性检查
            sb.Append(IISSystemBINCheck(ref error));
            //配置文件权限检查
            if (!GetConfig())
            {
                sb.Append("<tr style=\"height:15px\"><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>CNVP.JShop.config文件没有写入权限！</td></tr>");
                error = true;
            }
            else
            {
                sb.Append("<tr style=\"height:15px\"><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>对CNVP.JShop.config验证通过！</td></tr>");
            }
            //系统目录权限检查
            string FolderStr = "Admin,Config/Cache,Config,UploadFile";
            foreach (string Foldler in FolderStr.Split(','))
            {
                if (!SystemFolderCheck(Foldler))
                {
                    sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>对" + Foldler + "目录没有写入和删除权限!</td></tr>");
                    error = true;
                }
                else
                {
                    sb.Append("<tr><td bgcolor='#ffffff' width='5%'><img src='images/ok.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>对" + Foldler + "目录权限验证通过!</td></tr>");
                }
            }
            sb.Append("</table>");

            return sb.ToString();
        }
        #region "检查系统Dll是否完整"
        /// <summary>
        /// 检查系统Dll是否完整
        /// </summary>
        /// <param name="error"></param>
        /// <returns></returns>
        public static string IISSystemBINCheck(ref bool error)
        {
            string binfolderpath = HttpRuntime.BinDirectory;

            string result = "";
            try
            {
                string[] assemblylist = new string[] { "CNVP.Config.dll", "CNVP.Framework.dll", "CNVP.JShop.BLL.dll", "CNVP.JShop.DAL.dll", "CNVP.JShop.Model.dll", "CNVP.JShop.Publish.dll", "FredCK.FCKeditorV2.dll", "CNVP.JShop.UI.dll", "CNVP.JShop.Web.dll", "PayWayAPI.dll" };
                bool isAssemblyInexistence = false;
                ArrayList inexistenceAssemblyList = new ArrayList();
                foreach (string assembly in assemblylist)
                {
                    if (!File.Exists(binfolderpath + assembly))
                    {
                        isAssemblyInexistence = true;
                        error = true;
                        inexistenceAssemblyList.Add(assembly);
                    }
                }
                if (isAssemblyInexistence)
                {
                    foreach (string assembly in inexistenceAssemblyList)
                    {
                        result += "<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>" + assembly + " 文件放置不正确<br/>请将所有的dll文件复制到目录 " + binfolderpath + " 中.</td></tr>";
                    }
                }
            }
            catch
            {
                result += "<tr><td bgcolor='#ffffff' width='5%'><img src='images/error.gif' width='16' height='16'></td><td bgcolor='#ffffff' width='95%'>请将所有的dll文件复制到目录 " + binfolderpath + " 中.</td></tr>";
                error = true;
            }

            return result;
        }
        #endregion
        #region "检查配置文件修改权限"
        public static bool GetConfig()
        {
            try
            {
                BaseConfig.SaveXmlConfig("SiteAdmin", "Admin");
                string strDate = DateTime.Now.ToString("yyyyMMddhhMMss");
                BaseConfig.SaveXmlConfig("CookiesName", "JShop_Admin" + strDate + "");
                return true;
            }
            catch(Exception ex)
            {
                LogHelper.Write("配置文件权限设置错误", ex.ToString());
                return false;
            }
        }
        #endregion
        #region 系统目录权限检查
        public static bool SystemFolderCheck(string foldername)
        {
            string physicsPath = HttpContext.Current.Server.MapPath("../" + foldername);
            try
            {
                using (FileStream fs = new FileStream(physicsPath + "\\a.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    fs.Close();
                }
                if (File.Exists(physicsPath + "\\a.txt"))
                {
                    System.IO.File.Delete(physicsPath + "\\a.txt");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
